export default {
	data () {
		return {
			list:[],
			selectGoods:[],
			selectData:[],
			orderChecked:true,
			loading_btn:false,
			isOpen:false,
			isDelete:true,
			isDeleteOrder:true
		}
	},
	watch:{
		
		// list:{
		// 	handler(newList){
		// 		for(let i in newList){
		// 			newList[i].isShow = true
		// 		}
		// 		this.list = newList
		// 	},
		// 	deep:true
		// }
	},
	computed: {
	    order() {
		  let str = 'new'
		  if(this.order_number){
			 str = this.$utils.sliceStr(this.order_number,6)
		  }
	      return str
	    },
		sumMoneyTotaled(){
			let moneyTotal = 0
			for(let item of this.list){
				moneyTotal+=item.price*item.goods_number
			}
			return moneyTotal.toFixed(2)
		},
		sumCountTotaled(){
			let countTotal = 0
			for(let item of this.list){
				if(item.goods_name){
					countTotal+=item.goods_number*1
				}
			}
			return countTotal
		}
	},
    mounted(){
		
		
		if(location.href.indexOf('kxm')!=-1){
			this.isOpen = true
		}else{
			this.isOpen = false
		}
	},
	methods: {
		jump(url){
			let location = window.location.href
			let href = location.split('#')[0]
			if(this.isOpen){
				 if(location.indexOf('/deskTop/home')!=-1){
					this.$router.push({path:url})
				 }else{
					 if(location.indexOf(url)==-1){
						  window.open(href+'#'+url)
					 }
				 }
			}else{
				this.$router.push({path:url})
			}
		},
		toInfo(row){
			let is_jc = 0
			if(this.is_jc){
				is_jc = 1
			}
			
			let type = row.type || row.status
			if(type==1){
				this.jump(`/buyerSys/buyerInfo?order_number=${row.order_number}&is_jc=${is_jc}`)
				// this.$router.push({
				// 	path:"/buyerSys/buyerInfo",
				// 	query:{
				// 		order_number:row.order_number,
				// 		is_jc:is_jc
				// 	}
				// })
			}
			if(type==0){
				this.jump(`/buyerSys/buyerReturn?order_number=${row.order_number}&is_jc=${is_jc}`)
				// this.$router.push({
				// 	path:"/buyerSys/buyerReturn",
				// 	query:{
				// 		order_number:row.order_number,
				// 		is_jc:is_jc
				// 	}
				// })
			}
			if(type==2){
				this.jump(`/wholesalerSys/wholesaler?order_number=${row.order_number}&is_jc=${is_jc}`)
				// this.$router.push({
				// 	path:"/wholesalerSys/wholesaler",
				// 	query:{
				// 		order_number:row.order_number,
				// 		is_jc:is_jc
				// 	}
				// })
			}
			if(type==3){
				this.jump(`/wholesalerSys/wholesalerReturn?order_number=${row.order_number}&is_jc=${is_jc}`)
				// this.$router.push({
				// 	path:"/wholesalerSys/wholesalerReturn",
				// 	query:{
				// 		order_number:row.order_number,
				// 		is_jc:is_jc
				// 	}
				// })
			}
			
		},
		
		newAdd(type=0){
			this.form.supplier = ''
			this.form.logistics_number = ''
			 this.order_number = ''
			 this.isDelete = true
			this.list = []
			this.$router.replace({
				path:this.$route.path,
				query:{
					
				}
			})
			setTimeout(()=>{
				this.initView()
			},200)
			
			if(type==1){
				this.$notify.closeAll()
			}
			
		},
		delRow(scope){
			// this.$refs.multipleTable.clearSelection()
			let index = scope.$index
			let ids = []
			
			if(this.list[index].id){
				ids = [this.list[index].id]
			}
			if(ids.length){
				this.delOrder(ids)
			}
			this.list.splice(index,1)
			this.isShowSelectGoods = false
			this.$nextTick(()=>{
				this.isShowSelectGoods = true
			})
			
			// if(this.selectDelIds.length==0) return this.$message.error('请选择要删除的商品')
			
			// let ids = []
			// for(let del_id of this.selectDelIds){
			// 	let index = this.list.findIndex((ele)=>{
			// 		return ele.del_id = del_id
			// 	})
			// 	if(this.list[index].id){
			// 		ids.push(this.list[index].id)
			// 	}
			// 	// this.list.splice(index,1);
			// }
			
			// this.list = this.list.filter((item, index) => {
			//   return !this.selectDelIds.includes(item.del_id);
		 //    });
			
			// this.isShowSelectGoods = false
			// this.$nextTick(()=>{
			// 	this.isShowSelectGoods = true
			// })
			
			
			
		},
		//删除订单
		delOrder(ids,ind=-1){
			
			let isDeleteOrder = sessionStorage.getItem('isDeleteOrder')
			if(isDeleteOrder==0 && ind==-1){
				return this.$message.error('你没有删除权限')
			}
			let order_number = this.$route.query.order_number
			
			if(ind>-1){
				let order_number = this.$route.query.order_number
				 let params = {
					 order_number:order_number,
					 type:this.type,
					 is_jc:this.is_jc
				 }
				 if(ids){
					params.ids = ids
				 }else{
					params.ids = ''
				 }
				this.$post('/buyerSys/delBuyerInfo',params,res=>{
					 if(res.data.code==200){
						 // this.initView()
					 }
				})
			}else{
				if(order_number){
					this.$confirm('确定删除?', '提示', {
						  confirmButtonText: '确定',
						  cancelButtonText: '取消',
						  type: 'warning'
						}).then(() => {
						  let order_number = this.$route.query.order_number
						  let params = {
							 order_number:order_number,
							 type:this.type,
							 is_jc:this.is_jc
						  }
						  if(ids){
							  params.ids = ids
						  }else{
							   params.ids = ''
						  }
						 this.$post('/buyerSys/delBuyerInfo',params,res=>{
							
							 if(res.data.code==200){
								 this.initView()
							 }
						 })
						  
					}).catch(() => {
						this.initView()
					});
				}else{
					this.initView()
				}
			}
			
			
		},
		//查找下一条上一条
		prevNextBtn(type,handerType=1){
			let is_jc = 0
			if(this.is_jc){
				is_jc=1
			}
			let params = {
				order_number:this.$route.query.order_number || -1,
				type:type,
				handerType:handerType,
				is_jc:is_jc
			}
			this.$post('/buyerSys/prevNextBtn',params,res=>{
				let result = res.data
				if(result.code==200){
					let order_number = result.data.order_number_new
					if(order_number){
						this.$router.replace({
							path:this.$route.path,
							query:{
								order_number:order_number,
								is_jc:is_jc
							}
						})
						this.initView()
					}
				}
				
			})
		},
		 copyRow(){
			 for(let item of this.selectGoods){
				 let obj = {
				 	id:undefined,
				    goods_name:item.goods_name,
				 	goods_number:item.goods_number,
				 	price:item.price,
				 	money:item.money,
				 	codeArr:[],
					codeArray:[],
				 	remark:item.remark,
				 	brand:item.brand,
				 	goods_type:item.goods_type,
				 	number_length:undefined
				 }
				 this.list.push(obj)
			 }
			 this.isShowSelectGoods = false
			 this.$nextTick(()=>{
			 	this.isShowSelectGoods = true
			 })
		 },
		 delLastList(){
			 let len = this.list.length
			 let ids = []
			 if(this.list[len-1].id){
				 ids.push(this.list[len-1].id)
				 this.delOrder(ids)
			 }else{
				 this.list.splice(len-1,1)
			 }
			 
		 },
		 cellClick(row, column, cell, event){
			 this.$refs.multipleTable.toggleRowSelection(row,true);
		 },
		 pushList(scope){
		 	let len = this.list.length
		 	let index = scope.$index
		 	if(len-1 == index){
		 		this.addBtn()
		 	}
		 },
		 handleSelectionChange(val){
		 	this.selectGoods = [...val]
			this.selectData = [...val]
		 },
		 pageChange(pageSize){
		     this.page=1
		     this.pageSize=pageSize
		     this.lodeList()
		 },
		 handleCurrentChange(page){
		     this.page=page
		     this.lodeList()
		 },
		 search(){
		     this.page = 1
		 	this.lodeList()
		 },
	}
}